package com.yc.starter.login.util;

import com.yc.starter.login.biz.BizException;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

public class Utils {
    public static boolean isEmpty(Object value) {
        if (value == null) {
            return true;
        } else if (value instanceof String) {
            return ((String)value).isBlank();
        } else if (value instanceof Collection) {
            return ((Collection)value).isEmpty();
        } else if (value instanceof Map) {
            return ((Map)value).isEmpty();
        } else if (value.getClass().isArray()) {
            return Array.getLength(value) == 0;
        } else {
            return false;
        }
    }

    public static boolean isNotEmpty(Object value) {
        return !isEmpty(value);
    }

    public static <T, R> T obtain(Supplier<T> getter, T defaultValue) {
        try {
            T t = (T)getter.get();
            return (T)(t != null ? t : defaultValue);
        } catch (RuntimeException e) {
            System.err.println(e.getMessage());
            return defaultValue;
        }
    }

    public static Checker check(boolean condition, String message, Object... args) {
        return (new Checker(false)).check(condition, message, args);
    }

    public static Checker check(Supplier<Boolean> tester, String message, Object... args) {
        return (new Checker(false)).check(tester, message, args);
    }

    public static Checker checkEmpty(Object value, String message, Object... args) {
        return check(isEmpty(value), message, args);
    }

    public static Checker checks(boolean condition, String message, Object... args) {
        return (new Checker(true)).check(condition, message, args);
    }

    public static Checker checks(Supplier<Boolean> tester, String message, Object... args) {
        return (new Checker(true)).check(tester, message, args);
    }

    public static Checker checkEmptys(Object value, String message, Object... args) {
        return checks(isEmpty(value), message, args);
    }

    public static class Checker {
        private List<Map<String, Object>> errors;

        public Checker(boolean collectMessage) {
            if (collectMessage) {
                this.errors = new ArrayList();
            }

        }

        public List<Map<String, Object>> getErrors() {
            return this.errors;
        }

        public boolean hasErrors() {
            return this.errors != null && this.errors.size() > 0;
        }

        public void clear() {
            if (this.errors != null) {
                this.errors.clear();
            }

        }

        public void throwErrors() {
            if (this.hasErrors()) {
                throw (new BizException("校验失败")).setData(this.errors);
            }
        }

        public Checker check(boolean condition, String message, Object... args) {
            if (condition) {
                if (this.errors == null) {
                    throw new BizException(message);
                }

                Map<String, Object> map = new LinkedHashMap();
                map.put("message", message);
                if (args.length >= 1) {
                    map.put("name", args[0]);
                }

                if (args.length >= 2) {
                    map.put("value", args[1]);
                }

                this.errors.add(map);
            }

            return this;
        }

        public Checker check(Supplier<Boolean> tester, String message, Object... args) {
            boolean ret;
            try {
                ret = (Boolean)tester.get();
            } catch (Exception e) {
                e.printStackTrace();
                ret = true;
            }

            return this.check(ret, message, args);
        }

        public Checker checkEmpty(Object value, String message, Object... args) {
            if (Utils.isEmpty(value)) {
                if (this.errors == null) {
                    throw new BizException(message);
                }

                Map<String, Object> map = new LinkedHashMap();
                map.put("message", message);
                if (args.length >= 1) {
                    map.put("name", args[0]);
                }

                if (args.length >= 2) {
                    map.put("value", args[1]);
                }

                this.errors.add(map);
            }

            return this;
        }

        public Checker printErrors() {
            if (this.errors != null) {
                for(Map<String, Object> error : this.errors) {
                    System.out.println(error);
                }
            }

            return this;
        }
    }
}

